package action.util;

import java.util.ArrayList;
import java.util.List;

import action.pojo.LocalFilePojo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class FavoriteConnectSQLite {
	private SQLiteDatabase database;
	private DatabaseHelper helper;
	private String[] allColumns = {DatabaseHelper.COLUMN_ID,DatabaseHelper.COLUMN_NAME,DatabaseHelper.COLUMN_PATH,DatabaseHelper.COLUMN_FOLDER};
	
	private LocalFilePojo filePojo ;
	
	public FavoriteConnectSQLite(Context context){
		helper = new DatabaseHelper(context);
	}
	
	public void open() throws SQLException {
		database = helper.getWritableDatabase();
	}

	public void close() {
		helper.close();
	}
	
	public LocalFilePojo createNewFavorite(LocalFilePojo file) {
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.COLUMN_NAME, file.getFileItem().getName());
		values.put(DatabaseHelper.COLUMN_PATH, file.getFileItem().getAbsolutePath());
		if(file.getFileItem().isDirectory()){
			values.put(DatabaseHelper.COLUMN_FOLDER,0);	
		}else{
			values.put(DatabaseHelper.COLUMN_FOLDER,1);
		}
		
		
		long insertId = database.insert(DatabaseHelper.TABLE_FAVORITE, null,
				values);
		Cursor cursor = database.query(DatabaseHelper.TABLE_FAVORITE,
				allColumns, DatabaseHelper.COLUMN_ID + " = " + insertId, null,
				null, null, null);
		cursor.moveToFirst();
		LocalFilePojo newFilePojo = cursorToFavorite(cursor);
		cursor.close();
		return newFilePojo;
	}
	
	
	
	public List<LocalFilePojo> getAllConnect() {
		List<LocalFilePojo> ftpConnects = new ArrayList<LocalFilePojo>();

		Cursor cursor = database.query(DatabaseHelper.TABLE_FAVORITE,
				allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			 filePojo = cursorToFavorite(cursor);
			ftpConnects.add(filePojo);
			
			System.out.println("lst " + filePojo.getName());
			
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return ftpConnects;
	}
	
	public void deleteFavorite() {
		database.delete(DatabaseHelper.TABLE_FAVORITE,null, null);
		close();
	}
	
	// update ftp
	public boolean updateFavorite(LocalFilePojo connect){
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.COLUMN_NAME, connect.getFileItem().getName());
		values.put(DatabaseHelper.COLUMN_PATH, connect.getPath());
		values.put(DatabaseHelper.COLUMN_FOLDER, connect.getIsFolder());
		return database.update(DatabaseHelper.TABLE_FAVORITE, values, DatabaseHelper.COLUMN_ID + "=" + connect.getIdfavorite(), null) >0;
	}
	
	public LocalFilePojo cursorToFavorite(Cursor cursor){
		 filePojo = new LocalFilePojo();
		try{
			filePojo.setIdfavorite(cursor.getInt(0));
			filePojo.setName(cursor.getString(1));
			filePojo.setPath(cursor.getString(2));
			filePojo.setIsFolder(cursor.getInt(3));
		}catch(Exception e){
			e.printStackTrace();
		}
		return filePojo;
	}
	
}
